<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'mo:order:scOrder:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="50%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }}</span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsMoOrderCxfpForm2',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed, onMounted } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  // import { ScOrder, scOrderSave, scOrderForm } from '/@/api/mes/mo/order/scOrder';
  import {
    MesOrderLinefp,
    mesOrderLinefpForm,
    mesOrderLinefpSave,
    mesOrderLinefpTreeData,
  } from '/@/api/mes/mo/line/mesOrderLinefp';
  import { basLineListTreeData } from '/@/api/bas/mes/workline/basLine';
  import { basWorkShopTreeData } from '/@/api/bas/mes/workshop/basMesWorkShop';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('mo.order.scOrder');
  const { showMessage } = useMessage();
  const record = ref<MesOrderLinefp>({} as MesOrderLinefp);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: t('修改产线分配'),
  }));
  const dynamicParams = computed(() => {
    const workShopCode = record.value?.scOrder?.workShopCode;
    return workShopCode
      ? { 'basWorkShop.officeId': workShopCode, isShowCode: false, isAll: false }
      : { isShowCode: false, isAll: true };
  });
  const inputFormSchemas: FormSchema[] = [
    {
      label: t('生产线'),
      field: 'lineCode',
      component: 'TreeSelect',
      componentProps: {
        api: basLineListTreeData,
        params: dynamicParams,
        allowClear: true,
        canSelectParent: false,
        //immediate: true,
      },
      required: true,
    },
    // {
    //   label: t('生产线'),
    //   field: 'lineCode',
    //   fieldLabel: 'lineName',
    //   component: 'TreeSelect',
    //   componentProps: {
    //     api: mesOrderLinefpTreeData,
    //     // allowClear: true,
    //   },
    //   itemProps:{ validateTrigger: 'blur' },
    //   required: true,
    // },
    {
      label: t('数量'),
      field: 'iqty',
      component: 'InputNumber',
      componentProps: {
        // maxlength: 8,
      },
      rules: [{ required: true, pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('备注'),
      field: 'remarks',
      component: 'InputTextArea',
      componentProps: {
        maxlength: 64,
        rows: 8,
      },
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 24, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await mesOrderLinefpForm(data);
    record.value = (res.mesOrderLinefp || {}) as MesOrderLinefp;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
      };
      // console.log('submit', params, data, record);
      const res = await mesOrderLinefpSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
